package com.disapand.dingdang.extension

import com.disapand.dingdang.vo.PageVO
import org.ktorm.dsl.Query
import org.ktorm.dsl.limit
import org.ktorm.dsl.map
import org.ktorm.schema.BaseTable

/**
 * @author disapand
 * @date 2022/3/9 - 21:02 星期三
 */

/**
 * query 方式的分页扩展
 *
 * @param E
 * @param T
 * @param current
 * @param pageSize
 * @param entity
 * @return
 */
fun <E : Any, T : BaseTable<E>> Query.page(current: Int, pageSize: Int, entity: T): PageVO<E> {
    val total = this.totalRecords
    val list = this.limit((current - 1) * pageSize, pageSize).map { row -> entity.createEntity(row) }
    return PageVO(
        total,
        current,
        pageSize,
        list
    )
}